﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace Bank.Operations
{
    public class Transfer : Operation
    {
        IAccount _destinationAccount;

        decimal _amountToTransfer;

        public Transfer(IAccount destinationAccount, decimal amountToTransfer)
        {
            _destinationAccount = destinationAccount;

            _amountToTransfer = amountToTransfer;
        }

        public override void ExecuteOperation(IAccount account)
        {
            // we need to perform this operation in transaction
            // we need to make sure that this operation is thread safe

            Contract.Requires(account.Ballance >= _amountToTransfer);

            _destinationAccount.Ballance += _amountToTransfer;

            account.Ballance -= _amountToTransfer;

            base.ExecuteOperation(account);
        }

        public override void ExecuteOperation2(IAccount account)
        {
            // we need to perform this operation in transaction
            // we need to make sure that this operation is thread safe

            Contract.Requires(account.Ballance2 >= _amountToTransfer);

            _destinationAccount.Ballance2 += _amountToTransfer;

            account.Ballance2 -= _amountToTransfer;

            base.ExecuteOperation(account);
        }

        public override string GetOperationName()
        {
            return GetType().Name;
        }
    }
}
